package org.jash.learnmvp.news;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import org.jash.learnmvp.R;
import org.jash.learnmvp.net.ApiService;
import org.jash.learnmvp.utils.NetUtils;

import java.util.HashMap;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CommentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CommentFragment extends BottomSheetDialogFragment {

    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_ID = "id";

    private int id;
    private RecyclerView recycler;
    private EditText content;
    private CommentAdapter adapter = new CommentAdapter();
    private ApiService service = NetUtils.getRetrofit().create(ApiService.class);
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param id Parameter 1.
     * @return A new instance of fragment CommentFragment.
     */
    public static CommentFragment newInstance(int id) {
        CommentFragment fragment = new CommentFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_ID, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getInt(ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_comment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler = view.findViewById(R.id.recycler);
        content = view.findViewById(R.id.content);
        recycler.setAdapter(adapter);
        loadComment();
        view.findViewById(R.id.send).setOnClickListener(v -> {
            service.sendComment(new HashMap<String, Object>() {{
                put("content", content.getText().toString());
                put("nid", id);
                put("parentid", 0);
            }}).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(res -> {
                        if (res.getCode() == 0) {
                            loadComment();
                        } else {
                            Toast.makeText(requireContext(), res.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    });
        });
    }
    public void loadComment() {
        service.getComments(id).observeOn(AndroidSchedulers.mainThread())
                .subscribe(res -> {
                    if (res.getCode() == 0) {
                        adapter.clear();
                        adapter.addAll(res.getData());
                    }
                });
    }
}